from marshmallow import Schema, fields, validate, validates, ValidationError


class UserCreateSchema(Schema):
    """用户创建模式"""
    username = fields.String(required=True, validate=validate.Length(min=3, max=50))
    email = fields.Email(required=True)
    password = fields.String(required=True, validate=validate.Length(min=6))
    role = fields.String(validate=validate.OneOf(['admin', 'user']), default='user')
    
    @validates('username')
    def validate_username(self, value):
        # 这里可以添加更复杂的验证逻辑，如检查用户名是否已存在
        pass


class UserUpdateSchema(Schema):
    """用户更新模式"""
    id = fields.Integer(required=True)
    username = fields.String(validate=validate.Length(min=3, max=50))
    email = fields.Email()
    password = fields.String(validate=validate.Length(min=6))
    role = fields.String(validate=validate.OneOf(['admin', 'user']))


class UserSchema(Schema):
    """用户模式（用于序列化输出）"""
    id = fields.Integer(dump_only=True)
    username = fields.String()
    email = fields.Email()
    role = fields.String()
    created_at = fields.DateTime(dump_only=True)
    updated_at = fields.DateTime(dump_only=True) 